package zhu;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class Login extends JFrame {
    private JTextField studentIdField;
    private JPasswordField passwordField;

    public Login() {
        setTitle("登录");
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置默认关闭操作
        setLocationRelativeTo(null);
        setLayout(new GridLayout(3, 2, 5, 5)); // 添加间距

        add(new JLabel("学号："));
        studentIdField = new JTextField();
        add(studentIdField);

        add(new JLabel("密码："));
        passwordField = new JPasswordField();
        add(passwordField);

        JButton loginButton = new JButton("登录");
        add(loginButton);

        JButton registerButton = new JButton("注册");
        add(registerButton);

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginUser();
            }
        });

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Register().setVisible(true);
                dispose(); // 关闭登录窗口
            }
        });
    }

 // ...（其他代码保持不变）...

    private void loginUser() {
        String studentId = studentIdField.getText();
        String password = new String(passwordField.getPassword());

        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE student_id = ? AND password = ?")) {

            stmt.setString(1, studentId);
            stmt.setString(2, password);

            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                String role = rs.getString("role");
                if ("管理员".equals(role)) {
                    // 确保在事件调度线程中创建和显示窗口
                    SwingUtilities.invokeLater(() -> {
                        AdminPanel adminPanel = new AdminPanel();
                        adminPanel.setVisible(true);
                    });
                    dispose(); // 关闭登录窗口
                } else if ("学生".equals(role)) {
                    // 确保在事件调度线程中创建和显示窗口
                    SwingUtilities.invokeLater(() -> {
                        StudentPanel studentPanel = new StudentPanel();
                        studentPanel.setVisible(true);
                    });
                    dispose(); // 关闭登录窗口
                }
            } else {
                JOptionPane.showMessageDialog(this, "无效的用户名或密码！");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "登录错误：" + ex.getMessage());
        }
    }

    // ...（其他代码保持不变）...

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                new Login().setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, "启动应用程序错误：" + e.getMessage());
            }
        });
    }
}

